import request from "@/utils/request"
import { useRequest } from "ahooks"
import { createGlobalStore } from "hox"
import { useInitStore } from "./initStore"

/**
 * @description 查询品牌信息
 */
async function getBrandList(): Promise<{list: Brand.Item[]}> {
  return request.get('/api/Common/GetBrandListNew')
}

export const [useBrandListStore, getBrandListStore] = createGlobalStore(() => { 
  const { initReady } = useInitStore()

  const getBrandListApi = useRequest(async() => ((await getBrandList())?.list || []).sort((a, b) => a.sort - b.sort), {
    ready: initReady,
  })

  return {
    brandList: getBrandListApi.data,
    loading: getBrandListApi.loading,
  }
})